﻿using AutoMapper;
using GGetTestService.Dto.Student;
using GGetTestService.Entity;
using GGetTestService.Extension;
using GGetTestService.Repository;
using GGetTestService.Repository.UnitOfWork;
using GGetTestService.Services.Base;
using GGetTestService.Services.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace GGetTestService.Services
{
    public class TeacherService : BaseService<Teacher>, ITeacherService
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IMapper _mapper;
        private IRepository<Teacher> _teacherRepository;

        public TeacherService(IUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _mapper = mapper;
            _teacherRepository = _unitOfWork.GetRepository<Teacher>();
        }

        public async Task AddTeacherAsync(Teacher teacher)
        {
            //方式一:
            //await base.AddAsync(teacher);

            //方式二:
            await _teacherRepository.AddAsync(teacher);
            await _unitOfWork.SaveChangesAsync();
        }

        public async Task<bool> ExistAsync(Guid? teacherId)
        {
            return await _teacherRepository.ExistAsync(x => x.Id == teacherId);
        }

        public async Task<IEnumerable<Teacher>> GetTeacherListAsync()
        {
            var list = await _teacherRepository.Queryable.Include(x => x.Students)
                                               .ToListAsync();

            var pp = await _teacherRepository.Queryable.OrderByBatch("Name").Include(x => x.Students)
                                             .ToListAsync();

            {
                var teacherQueryable = _teacherRepository.Queryable;
                var studentQueryable = _unitOfWork.GetRepository<Student>().Queryable;

                //var list1 = await (from t in teacherQueryable
                //                join s in studentQueryable
                //                on t.Id equals s.TeacherId into SS
                //                from s1 in SS.DefaultIfEmpty()
                //                select t).ToListAsync();

                //Id = t.Id,
                //                       Name = t.Name,
                //                       Age = t.Age,
                //                       Gender = t.Gender.ToString(),
                //                       Students = studentQueryable.Where(x => x.TeacherId == t.Id).ProjectTo<StudentDto>(_mapper).ToList()

                var list2 = await (from t in teacherQueryable
                                   join s in studentQueryable
                                   on t.Id equals s.TeacherId into SS
                                   from s1 in SS.DefaultIfEmpty()
                                   select new
                                   {
                                       Teacher = t,
                                       Student = s1
                                   }).ToListAsync();

                var ddd = list2.GroupBy(x => new { x.Teacher })
                    .Select(x => new
                    {
                        Teacher = x.Key,
                        Students = list2.Where(y => y.Student?.TeacherId == x.Key.Teacher.Id)
                        .Select(m => m.Student)
                        .ProjectTo<StudentDto>(_mapper)
                        .ToList()
                    }).ToList();
            }

            return list;
        }
    }
}